package com.dateConstruction._05_triple;

import com.dateConstruction._02_polynormal.Addible;
// 稀疏矩阵非零元素三元组类
public class Triple implements Comparable<Triple>, Addible<Triple> {

    // 行号，列号，元素值
    private int row, column, value;

    public int getRow() {
        return row;
    }

    public void setRow(int row) {
        this.row = row;
    }

    public int getColumn() {
        return column;
    }

    public void setColumn(int column) {
        this.column = column;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    // 构造方法
    public Triple(int row, int column, int value) {
        
        if (row >= 0 && column >= 0) {
            this.row = row;
            this.column = column;
            this.value = value;
        } else {
            throw new IllegalArgumentException("行、列号不能为负数：row = " + row + "，column = " + column);
        }
    }

    // 以字符串构造，形式为"(,,)"，没有空格
    public Triple(String triple) {
        // 查找两个','字符的序号
        int i = triple.indexOf(','), j = triple.indexOf(',', i + 1);
        // 未处理数值格式异常
        this.row = Integer.parseInt(triple.substring(1, i));
        this.column = Integer.parseInt(triple.substring(i + 1, j));
        this.value = Integer.parseInt(triple.substring(j + 1, triple.length() - 1));
        if (this.row < 0 || this.column < 0) {
            throw new IllegalArgumentException("行、列号不能为负数：row = " + row + "，column = " + column);
        }
    }

    // 拷贝构造方法，复制一个三元组
    public Triple(Triple triple) {
        this(triple.row, triple.column, triple.value);
    }

    public String toString() {
        return "(" + row + "," + column + "," + value + ")";
    }

    // 返回矩阵对称位置元素的三元组
    public Triple toSymmetry() {
        return new Triple(this.column, this.row, this.value);
    }

    // 比较this与obj三元组是否相等，比较其位置和元素值
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj instanceof Triple) {
            Triple triple = (Triple) obj;
            return this.row == triple.row && this.column == triple.column && this.value == triple.value ? true : false;
        }
        return false;
    }

    @Override
    // 根据行列位置（行主序）比较三元组对象大小，与元素值无关，约定三元组排序次序
    public int compareTo(Triple triple) {
        // TODO Auto-generated method stub
        // 相等条件，与equals方法含义不同
        if (this.row == triple.row && this.column == triple.column) {
            return 0;
        }
        return (this.row < triple.row || this.row == triple.row && this.column < triple.column) ? -1 : 1;
    }
    
    @Override
    public void add(Triple term) {
        // TODO Auto-generated method stub
        this.value += term.value;
    }

    @Override
    public boolean removable() {
        // TODO Auto-generated method stub
        return this.value == 0;
    }
    
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Triple t = new Triple("(1,2,4)");
        System.out.println(t + ", " + t.toSymmetry());
        Triple t1 = new Triple(t);
        System.out.println(t1.equals(t) + ", " + t1.compareTo(t.toSymmetry()));
        
    }
}
